package cn.tonyandmoney.voicenotes.alarm

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import cn.tonyandmoney.anko.adapter.setKEmptyView
import cn.tonyandmoney.voicenotes.R
import cn.tonyandmoney.voicenotes.alarm.entity.TAlarm
import cn.tonyandmoney.voicenotes.alarm.utils.AlarmUtils
import cn.tonyandmoney.voicenotes.greendao.gen.TAlarmDao
import cn.tonyandmoney.voicenotes.greendao.utils.GreenDaoHelper
import cn.tonyandmoney.voicenotes.ui.component.lineDivider
import com.afollestad.materialdialogs.MaterialDialog
import com.zlzsy.app.base.ui.BaseActivity
import com.zlzsy.app.base.ui.adapter.KAdapter
import org.jetbrains.anko.*
import org.jetbrains.anko.recyclerview.v7.recyclerView

/**
 * 语音闹钟
 */
class VoiceAlarmActivity : BaseActivity() {

    private val mAdapter = KAdapter<TAlarm>()
    private val alarmDao = GreenDaoHelper.session().tAlarmDao

    override fun initView() {
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        verticalLayout {
            recyclerView {
                layoutManager = LinearLayoutManager(ctx)
                mAdapter.bindToRecyclerView(this)
            }.lparams(matchParent, matchParent)
        }
        mAdapter.setItemView {
            verticalLayout {
                relativeLayout {
                    padding = dip(15)
                    textView {
                        id = R.id.alarm_date
                        textSize = 20f
                    }.lparams(wrapContent, wrapContent)
                    textView {
                        id = R.id.alarm_cycle
                        textSize = 13f
                    }.lparams(wrapContent, wrapContent) {
                        rightOf(R.id.alarm_date)
                        sameBottom(R.id.alarm_date)
                        leftMargin = dip(10)
                    }
                    textView {
                        id = R.id.alarm_memo
                        textSize = 14f
                    }.lparams(wrapContent, wrapContent) {
                        bottomOf(R.id.alarm_date)
                        topMargin = dip(10)
                    }
                }.lparams(matchParent, wrapContent)
                lineDivider()
                lparams(matchParent, wrapContent)
            }
        }.setDataBind { holder, tAlarm ->
            holder.setText(R.id.alarm_date, tAlarm.date)
            holder.setText(R.id.alarm_memo, tAlarm.name)
            holder.setText(R.id.alarm_cycle, AlarmUtils.parseRepeat(tAlarm.cycle, 0))
        }.setKEmptyView(this) {
            frameLayout {
                lparams(matchParent, matchParent)
                textView {
                    textSize = 18f
                    text = "还没有建立闹钟哦"
                }.lparams(wrapContent, wrapContent) {
                    gravity = Gravity.CENTER
                }
            }
        }
    }


    /**
     * 这个地方需要初始化数据库
     */
    override fun initData(savedInstanceState: Bundle?) {
        mAdapter.setOnItemClickListener { _, _, position ->
            val entity = mAdapter.getItem(position) ?: return@setOnItemClickListener
            startActivity<CreateAlarmActivity>("id" to entity.id)
        }
        mAdapter.setOnItemLongClickListener { _, _, position ->
            val entity = mAdapter.getItem(position) ?: return@setOnItemLongClickListener false
            MaterialDialog.Builder(this)
                    .title("提示")
                    .content("是否要删除该闹钟？")
                    .positiveText("确定")
                    .negativeText("取消")
                    .onPositive { _, _ ->
                        alarmDao.delete(entity)
                        mAdapter.remove(position)
                    }
                    .show()
            return@setOnItemLongClickListener true
        }
        onRefresh()
    }

    private fun onRefresh() {
        val builder = alarmDao.queryBuilder()
                .orderDesc(TAlarmDao.Properties.Id)
                .build()
        mAdapter.setNewData(builder.list())
    }

    override fun onResume() {
        super.onResume()
        initData(null)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.alarm_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.menu_alarm_create) {
            startActivity<CreateAlarmActivity>()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}